import { StoreOptions } from "vuex";
import ACCESS_ENUM from "@/access/ACCESS_ENUM";
import { UserControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";
import store from "@/store/index";

const getters = {};

export default {
  namespaced: true,
  // 存储的状态信息
  state: () => ({
    loginUser: {
      userName: "未登录",
      userRole: ACCESS_ENUM.FIRET,
    },
  }),
  // 便捷获取变量的一些方法
  getters,
  // 执行异步操作并且触发 mutations 的修改 支持异步
  actions: {
    // 远程请求登陆用户信息
    async getLoginUser({ commit }) {
      const res = await UserControllerService.getLoginUserUsingGet();
      if (res.code === 200) {
        commit("updateUser", res.data);
      } else {
        commit("updateUser", {
          userName: "未登录",
          userRole: ACCESS_ENUM.NOT_LOGIN,
        }); // console.log("store.state.user.loginUser", store.state.user.loginUser);
        // message.error("获取登陆用户失败:" + res.message);
      }
    },
  },
  // 修改状态变量 更新变量的方法 尽量同步
  mutations: {
    updateUser(state, payload) {
      state.loginUser = payload;
    },
  },
} as StoreOptions<any>;
